/**
 * 
 */
package com.java.test.paymanage.service.impl;

import com.java.test.mapper.AreaMapper;
import com.java.test.paymanage.dto.*;
import com.java.test.paymanage.mapper.*;
import com.java.test.paymanage.service.PayManageService;
import com.java.test.system.feesettings.entity.FundsTypeEntity;
import com.java.test.system.registrationtemplate.entity.AreaEntity;
import com.java.test.timer.MessagePushTimer;
import com.java.test.user.dto.MemberSliceInfo;
import com.java.test.util.RequestGameServiceUtil;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author Administrator
 *
 */
@Slf4j
@Service
public class PayManageServiceImpl implements PayManageService {

	@Autowired
	private PayTypeMapper payTypeMapper;

	@Autowired
	private PayChannelMapper payChannelMapper;

	@Autowired
	private SkAccountMapper skAccountMapper;

	@Autowired
	private RechargeDiscountMapper rechargeDiscountMapper;

	@Autowired
	private OnlinePayMapper onlinePayMapper;

	@Autowired
	private AreaMapper areaMapper;

	@Value("${file.img.down.url}")
	private String fielUrl;

	@Value("${get.game.offLineUser.url}")
	private String offLineUserUrl;

	@Override
	public List<PayTypeInfo> selectpaytypeList() {
		return payTypeMapper.selectpaytypeList();
	}

	@Override
	public int insertpayType(PayTypeInfo info) {
		int i = payTypeMapper.insertpayType(info);
		if (i > 0) {
			MessagePushTimer.payTypeCLQ.offer("payTypePush");
		}
		return i;
	}

	@Override
	public int updatepayType(PayTypeInfo info) {
		int i = payTypeMapper.updatepayType(info);
		if (i > 0) {
			MessagePushTimer.payTypeCLQ.offer("payTypePush");
		}
		return i;
	}

	@Override
	public int deletepayType(PayTypeInfo info) {
		int i = payTypeMapper.deletepayType(info);
		if (i > 0) {
			MessagePushTimer.payTypeCLQ.offer("payTypePush");
		}
		return i;
	}

	@Override
	public List<PayChannelInfo> selectpaychanneList(PayChannelInfo info) {

		List<PayChannelInfo> list = payChannelMapper.selectpaychanneList(info);

		if (list != null && list.size() > 0) {
			list.forEach(b -> {
				b.setIconUrl(fielUrl + b.getIconUrl());
			});
			
			
			// 
			
			
			
		}

		return list;
	}

	@Override
	public int insertpayChannelInfo(PayChannelInfo info) {
		int i = payChannelMapper.insertpayChannelInfo(info);
		if (i > 0) {
			MessagePushTimer.payChannelCLQ.offer("payChannelPush");
		}
		return i;
	}

	@Override
	public int updatepayChannelInfo(PayChannelInfo info) {
		int i = payChannelMapper.updatepayChannelInfo(info);
		if (i > 0) {
			MessagePushTimer.payChannelCLQ.offer("payChannelPush");
		}
		return i;
	}

	@Override
	public int deletepayChannelInfo(PayChannelInfo info) {
		int i = payChannelMapper.deletepayChannelInfo(info);
		if (i > 0) {
			MessagePushTimer.payChannelCLQ.offer("payChannelPush");
		}
		return i;
	}

	@Override
	public List<SkAccountInfo> selectskaccount(SkAccountInfo info) {
		List<SkAccountInfo> list = skAccountMapper.selectskaccount(info);

		if (list == null || list.size() <= 0)
			return null;

		list.forEach(b -> {

			String areaids = b.getAreaId();

			if (areaids == null || "".equals(areaids))
				return;

			List<AreaEntity> arealist = areaMapper.selectAreaNamebyId(areaids);

			if (arealist == null || arealist.size() <= 0)
				return;

			String areaName = "";

			for (AreaEntity c : arealist) {
				areaName += c.getName() + ",";

			}
			areaName = areaName.substring(0, areaName.length() - 1);

			b.setAreaName(areaName);

		});

		list.forEach(b -> {

			String moneyid = b.getMoneyTypeId();

			if (moneyid == null || "".equals(moneyid))
				return;

			List<FundsTypeEntity> foundslist = areaMapper.selectfoundsType(moneyid);

			if (foundslist == null || foundslist.size() <= 0)
				return;

			String foundsName = "";

			for (FundsTypeEntity c : foundslist) {
				foundsName += c.getFullName() + ",";

			}
			foundsName = foundsName.substring(0, foundsName.length() - 1);

			b.setMoenyTypeName(foundsName);

		});

		list.forEach(b -> {

			String slices = b.getSliceId();

			if (slices == null || "".equals(slices))
				return;

			List<MemberSliceInfo> memberslicelist = areaMapper.selectMemberSliceInfo(slices);

			if (memberslicelist == null || memberslicelist.size() <= 0)
				return;

			String sliceName = "";

			for (MemberSliceInfo c : memberslicelist) {
				sliceName += c.getSliceName() + ",";

			}
			sliceName = sliceName.substring(0, sliceName.length() - 1);

			b.setSliceName(sliceName);

		});

		list.forEach(b -> {

			int channelid = b.getChannelId();

			if (channelid == 0)
				return;

			PayChannelInfo infos = payChannelMapper.selectpaychannelbyId(channelid);

			if (infos == null)
				return;

			b.setChannelName(infos.getChannelName());

		});

		return list;
	}

	@Override
	public int insertskaccount(SkAccountInfo info) {
		int i = skAccountMapper.insertskaccount(info);
		if (i > 0) {
			MessagePushTimer.payChannelCLQ.offer("payChannelPush");
		}
		return i;
	}

	@Override
	public int deleteskaccount(SkAccountInfo info) {
		int i = skAccountMapper.deleteskaccount(info);
		if (i > 0) {
			MessagePushTimer.payChannelCLQ.offer("payChannelPush");
		}
		return i;
	}

	@Override
	public int updateskaccount(SkAccountInfo info) {
		int i = skAccountMapper.updateskaccount(info);
		if (i > 0) {
			MessagePushTimer.payChannelCLQ.offer("payChannelPush");
		}
		return i;
	}

	@Override
	public int updateskaccountsort(List<SkAccountInfo> info) {
		int i = skAccountMapper.updateskaccountsort(info);
		if (i > 0) {
			MessagePushTimer.payChannelCLQ.offer("payChannelPush");
		}
		return i;
	}

	@Override
	public List<RechargeDiscountInfo> selectrechargediscountList() {
		return rechargeDiscountMapper.selectrechargediscountList();
	}

	@Override
	public int insertrechargediscountInfo(RechargeDiscountInfo info) {
		return rechargeDiscountMapper.insertrechargediscountInfo(info);
	}

	@Override
	public int deleterechargediscountInfo(RechargeDiscountInfo info) {
		return rechargeDiscountMapper.deleterechargediscountInfo(info);
	}

	@Override
	public int updaterechargediscountInfo(RechargeDiscountInfo info) {
		return rechargeDiscountMapper.updaterechargediscountInfo(info);
	}

	@Override
	public List<OnlinePayInfo> selectonlinepayList() {
		List<OnlinePayInfo> list = onlinePayMapper.selectonlinepayList();
		for (OnlinePayInfo entity : list) {
			PayTypeInfo info = payTypeMapper.selectById(entity.getPayChannelValue() + "");
			entity.setPaychannelName(info.getPayTypeName());
		}

		list.forEach(b -> {
			String slices = b.getOpenSlice();

			if (slices != null && !"".equals(slices)) {

				List<MemberSliceInfo> memberslicelist = areaMapper.selectMemberSliceInfo(slices);

				if (memberslicelist != null && memberslicelist.size() > 0) {

					String sliceName = "";

					for (MemberSliceInfo c : memberslicelist) {
						sliceName += c.getSliceName() + ",";

					}
					sliceName = sliceName.substring(0, sliceName.length() - 1);

					b.setOpenSliceName(sliceName);

				}

			}
		});

		return list;
	}

	@Override
	public int addonlinepayInfo(OnlinePayInfo info) {
		return onlinePayMapper.addonlinepayInfo(info);
	}

	public int updateonlinepayInfo(OnlinePayInfo info) {
		return onlinePayMapper.updateonlinepayInfo(info);
	}

	@Override
	public int updatesortonlinepayInfo(List<OnlinePayInfo> info) {
		return onlinePayMapper.updatesortonlinepayInfo(info);
	}

	@Override
	public int deleteonlinepayInfo(OnlinePayInfo info) {
		return onlinePayMapper.deleteonlinepayInfo(info);
	}

	@Override
	public SkAccountInfo skaccountInfo(SkAccountInfo info) {
		SkAccountInfo b = skAccountMapper.selectskaccountbyId(info);

		if (b != null) {
			String areaids = b.getAreaId();
			if (areaids != null || !"".equals(areaids)) {

				List<AreaEntity> arealist = areaMapper.selectAreaNamebyId(areaids);

				if (arealist != null && arealist.size() > 0) {

					String areaName = "";

					for (AreaEntity c : arealist) {
						areaName += c.getName() + ",";

					}
					areaName = areaName.substring(0, areaName.length() - 1);

					b.setAreaName(areaName);
				}
			}

			String moneyid = b.getMoneyTypeId();

			if (moneyid != null && !"".equals(moneyid)) {

				List<FundsTypeEntity> foundslist = areaMapper.selectfoundsType(moneyid);

				if (foundslist != null && foundslist.size() > 0) {

					String foundsName = "";

					for (FundsTypeEntity c : foundslist) {
						foundsName += c.getFullName() + ",";

					}
					foundsName = foundsName.substring(0, foundsName.length() - 1);

					b.setMoenyTypeName(foundsName);

				}

			}

			String slices = b.getSliceId();

			if (slices != null && !"".equals(slices)) {

				List<MemberSliceInfo> memberslicelist = areaMapper.selectMemberSliceInfo(slices);

				if (memberslicelist != null && memberslicelist.size() > 0) {

					String sliceName = "";

					for (MemberSliceInfo c : memberslicelist) {
						sliceName += c.getSliceName() + ",";

					}
					sliceName = sliceName.substring(0, sliceName.length() - 1);

					b.setMoenyTypeName(sliceName);

				}

			}

			int channelid = b.getChannelId();

			if (channelid != 0) {
				PayChannelInfo infos = payChannelMapper.selectpaychannelbyId(channelid);
				if (infos != null) {

					b.setChannelName(infos.getChannelName());
				}

			}

		}

		return b;
	}

	@Override
	public int updatepaysortType(List<PayTypeInfo> info) {
		int i = payTypeMapper.updatepaysortType(info);
		if (i > 0) {
			MessagePushTimer.payTypeCLQ.offer("payTypePush");
		}
		return i;
	}

	@Override
	public int updateskaccountstatus(SkAccountInfo info) {
		int i = skAccountMapper.updateskaccountstatus(info);
		if (i > 0) {
			MessagePushTimer.payChannelCLQ.offer("payChannelPush");
		}
		return i;
	}

	@Override
	public int updatepayChannelInfosort(List<PayChannelInfo> info) {
		int i = payChannelMapper.updatepayChannelInfosort(info);
		if (i > 0) {
			MessagePushTimer.payChannelCLQ.offer("payChannelPush");
		}
		return i;
	}

	/**
	 * 消息推送
	 */
	public void payMessagePush() {
		List<PayChannelInfo> list = payChannelMapper.selectpaychanneList(new PayChannelInfo());
		String time = new Date().getTime() + "";
		StringBuffer stra = new StringBuffer("[");
		for (PayChannelInfo entity : list) {
			StringBuffer str = new StringBuffer("\"");
			str.append(entity.getId()).append("\",");
			stra.append(str);
		}
		String data = "{\"code\":7,\"data\":" + stra.toString().substring(0, stra.length() - 1) + "]}";
		Random r1 = new Random();
		String url = RequestGameServiceUtil.createUrl(time, r1.nextInt(100) + "", data, offLineUserUrl);
		JSONObject resMsg = RequestGameServiceUtil.getService(url);
		String state = (resMsg.get("state").toString());
		if ("0".equals(state)) {
			log.info(resMsg.get("message").toString());
		} else {
			log.info(resMsg.get("message").toString());
		}
	}

	@Override
	public int updatestatusonlinepayInfo(OnlinePayInfo info) {

		return onlinePayMapper.updatestatusonlinepayInfo(info);
	}

	@Override
	public List<SkAccountInfo> selectallbankCard() {

		return skAccountMapper.selectallbankCard();
	}
	
	@Override
	public List<SkAccountInfo> selectallskAccount() {

		return skAccountMapper.selectallbankCard();
	}
	@Override
	public int updatepayTypestatusInfo(PayTypeInfo info) {
		int i = payTypeMapper.updatepayTypestatusInfo(info);
		if (i > 0) {
			MessagePushTimer.payTypeCLQ.offer("payTypePush");
		}
		return i;
	}

	@Override
	public int selectisnotexitSkaccount(int channelid) {
		return skAccountMapper.selectcountbychannelid(channelid);
	}

	@Override
	public List<PayChannelInfo> selectpaychannelbypaytypeId(int paytypeid) {
		return payChannelMapper.selectpaychannelbypaytypeId(paytypeid);
	}

}
